package com.hhj.controller;

import com.hhj.pojo.Bill;
import com.hhj.pojo.Provider;
import com.hhj.pojo.User;
import com.hhj.service.BillService;
import com.hhj.service.ProviderService;
import com.hhj.tools.Constants;
import com.hhj.tools.JsonUtils;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RequestMapping("/bill")
@Controller
public class BillController {

    @Autowired
    @Qualifier("billServiceImpl")
    private BillService billService;

    @Autowired
    @Qualifier("providerServiceImpl")
    private ProviderService providerService;

    @RequestMapping("/query")
    public String query(String queryProductName, String queryProviderId, String queryIsPayment, Model model) {
        if (StringUtils.isNullOrEmpty(queryProductName)) {
            queryProductName = "";
        }
        if (StringUtils.isNullOrEmpty(queryProviderId)) {
            queryProviderId = "0";
        }
        if (StringUtils.isNullOrEmpty(queryIsPayment)) {
            queryIsPayment = "0";
        }
        Bill bill = new Bill();
        bill.setProductName(queryProductName);
        bill.setProviderId(Integer.parseInt(queryProviderId));
        bill.setIsPayment(Integer.parseInt(queryIsPayment));
        List<Bill> billList = billService.getBillList(bill);
        List<Provider> providerList = providerService.getProviderList(null, null);
        for (int i = 0; i < billList.size(); i++) {
            for (int i1 = 0; i1 < providerList.size(); i1++) {
                if (billList.get(i).getProviderId()==providerList.get(i1).getId()){
                    billList.get(i).setProviderName(providerList.get(i1).getProName());
                }
            }
        }
        model.addAttribute("providerList", providerList);
        model.addAttribute("queryProductName", queryProductName);
        model.addAttribute("billList", billList);
        model.addAttribute("queryProviderId", queryProviderId);
        model.addAttribute("queryIsPayment", queryIsPayment);

        return "billlist";
    }

    @RequestMapping("/add")
    public String add(String billCode, String productName, String productUnit, String productCount, String totalPrice, String providerId, String isPayment, HttpSession session) {
        Bill bill = new Bill();
        bill.setBillCode(billCode);
        bill.setProductName(productName);
        bill.setProductUnit(productUnit);
        bill.setProductCount(new BigDecimal(productCount).setScale(2,BigDecimal.ROUND_DOWN));
        bill.setTotalPrice(new BigDecimal(totalPrice).setScale(2,BigDecimal.ROUND_DOWN));
        bill.setProviderId(Integer.parseInt(providerId));
        bill.setIsPayment(Integer.parseInt(isPayment));
        bill.setCreatedBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
        bill.setCreationDate(new Date());

        boolean add = billService.add(bill);

        return "redirect:/bill/query";
    }

    @ResponseBody
    @RequestMapping("/getproviderlist")
    public String getProviderList() {
        List<Provider> providerList = providerService.getProviderList(null, null);
        String json = JsonUtils.getJson(providerList);
        return json;
    }


    @RequestMapping("/tobillview")
    public String toBillView(String billid,Model model){
        Bill billById = billService.getBillById(billid);
        List<Provider> providerList = providerService.getProviderList(null, null);

            for (int i1 = 0; i1 < providerList.size(); i1++) {
                if (billById.getProviderId()==providerList.get(i1).getId()){
                    billById.setProviderName(providerList.get(i1).getProName());
                }
            }

        model.addAttribute("bill",billById);
        return "billview";
    }

    @RequestMapping("/toupdatebill")
    public String toUpdateBill(String billid,Model model){
        Bill billById = billService.getBillById(billid);
        model.addAttribute("bill",billById);
        return "billmodify";
    }

    @RequestMapping("/update")
    public String update(String id,String billCode, String productName, String productUnit, String productCount, String totalPrice, String providerId, String isPayment, HttpSession session){
        Bill bill = new Bill();
        bill.setId(Integer.parseInt(id));
        bill.setBillCode(billCode);
        bill.setProductName(productName);
        bill.setProductUnit(productUnit);
        bill.setProductCount(new BigDecimal(productCount).setScale(2,BigDecimal.ROUND_DOWN));
        bill.setTotalPrice(new BigDecimal(totalPrice).setScale(2,BigDecimal.ROUND_DOWN));
        bill.setProviderId(Integer.parseInt(providerId));
        bill.setIsPayment(Integer.parseInt(isPayment));
        bill.setModifyBy(((User)session.getAttribute(Constants.USER_SESSION)).getId());
        bill.setModifyDate(new Date());
        boolean modify = billService.modify(bill);
        System.out.println(modify);
        return "redirect:/bill/query";
    }

    @ResponseBody
    @RequestMapping("/delbill")
    public String delBill(String billid){
        Integer delId = Integer.parseInt(billid);
        HashMap<String, String> resultMap = new HashMap<String, String>();
        if(delId <= 0){
            resultMap.put("delResult", "notexist");
        }else{
            if(billService.deleteBillById(billid)){
                resultMap.put("delResult", "true");
            }else{
                resultMap.put("delResult", "false");
            }
        }
        return JsonUtils.getJson(resultMap);
    }

}
